package com.example.demo.common.code;

import com.example.demo.common.exception.YuException;
import org.apache.commons.lang3.StringUtils;

import javax.security.auth.login.LoginException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class PlatCodeStatement {
    protected static final Map<String, PlatStandardCode> CODE_CONTAINER = new HashMap();
    public static final PlatStandardCode SUCCESS;
    public static final PlatStandardCode SERVICE_IN_ROGRESS;
    public static final PlatStandardCode SERVICE_FLOW_BLOCKED;
    public static final PlatStandardCode SERVICE_DEGRADE_BLOCKED;
    public static final PlatStandardCode SERVICE_AUTH_BLOCKED;
    public static final PlatStandardCode SERVICE_SYSTEM_BLOCKED;
    public static final PlatStandardCode SYSTEM_UNKNOW_ERROR;
    public static final PlatStandardCode SERVICE_PERMISSION_INSUFFICIENT;
    public static final PlatStandardCode SERVICE_PERMISSION_ERROR;
    public static final PlatStandardCode SERVICE_PARAMS_MISSING;
    public static final PlatStandardCode SERVICE_PARAMS_INVALID;
    public static final PlatStandardCode SERVICE_FILEUPLOAD_FAIL;
    public static final PlatStandardCode BIZ_INVOKE_ERROR;
    public static final PlatStandardCode STANDARD_CODE_STATEMENT_REPEAT;
    public static final PlatStandardCode STANDARD_CODE_STATEMENT_ERROR;

    public PlatCodeStatement() {
    }

    protected static PlatStandardCode build(String alias, BasicCode basicCode, String subCode, String subDesc) {
        PlatStandardCode standardCode = new PlatStandardCode(alias, basicCode, subCode, subDesc);
        PlatStandardCode existed = (PlatStandardCode)CODE_CONTAINER.put(alias, standardCode);
        if (existed != null) {
            throw new YuException(BasicCode.ALREADY_EXIT.getCode(), BasicCode.ALREADY_EXIT.getCodeDesc());
        } else {
            return standardCode;
        }
    }

    protected static PlatStandardCode build(String alias, BasicCode basicCode, ExecuteFlag executeFlag, String subCode, String subDesc) {
        PlatStandardCode standardCode = new PlatStandardCode(alias, basicCode.getCode(), basicCode.getCodeDesc(), subCode, subDesc, executeFlag.getCode());
        PlatStandardCode existed = (PlatStandardCode)CODE_CONTAINER.put(alias, standardCode);
        if (existed != null) {
            throw new YuException(BasicCode.ALREADY_EXIT.getCode(), BasicCode.ALREADY_EXIT.getCodeDesc());
        } else {
            return standardCode;
        }
    }

    public static PlatStandardCode getWithAlias(String alias) {
        return StringUtils.isEmpty(alias) ? null : (PlatStandardCode)CODE_CONTAINER.get(alias);
    }

    public static Integer getPlatCodeSize() {
        return CODE_CONTAINER.size();
    }

    public static Map<String, PlatStandardCode> getPlatStandCodeMap() {
        Map<String, PlatStandardCode> result = new HashMap(CODE_CONTAINER.size());
        Iterator var1 = CODE_CONTAINER.keySet().iterator();

        while(var1.hasNext()) {
            String s = (String)var1.next();
            result.put(s, CODE_CONTAINER.get(s));
        }

        return result;
    }

    static {
        SUCCESS = build("success", BasicCode.SUCCESS, (String)null, (String)null);
        SERVICE_IN_ROGRESS = build("service.in.progress", BasicCode.SERVICE_IN_PROGRESS, "service-in-progress", "服务处理中");
        SERVICE_FLOW_BLOCKED = build("service.flow.blocked", BasicCode.SERVICE_ERROR, "service.flow.blocked", "服务流控阻断");
        SERVICE_DEGRADE_BLOCKED = build("service.degrade.blocked", BasicCode.SERVICE_ERROR, "service.degrade.blocked", "服务降级阻断");
        SERVICE_AUTH_BLOCKED = build("service.auth.blocked", BasicCode.SERVICE_ERROR, "service.auth.blocked", "服务授权阻断");
        SERVICE_SYSTEM_BLOCKED = build("service.system.blocked", BasicCode.SERVICE_ERROR, "service.system.blocked", "服务系统参数阻断");
        SYSTEM_UNKNOW_ERROR = build("system.unknown.error", BasicCode.SERVICE_ERROR, "system-unknown-error", "系统未知错误");
        SERVICE_PERMISSION_INSUFFICIENT = build("service.permission.insufficient", BasicCode.SERVICE_INSUFFICIENT_PERMISSION, "service-insufficient-permission", "服务权限不足");
        SERVICE_PERMISSION_ERROR = build("service.authentication.failed", BasicCode.SERVICE_INSUFFICIENT_PERMISSION, "service-authentication-failed", "服务鉴权失败");
        SERVICE_PARAMS_MISSING = build("service.params.missing", BasicCode.PARAMS_MISSING, "service-params-missing", "服务必要参数缺失");
        SERVICE_PARAMS_INVALID = build("service.params.invalid", BasicCode.PARAMS_INVALID, "service-params-invalid", "服务参数无效");
        SERVICE_FILEUPLOAD_FAIL = build("service.fileupload.failed", BasicCode.PARAMS_INVALID, "service-fileupload-failed", "文件上传失败");
        BIZ_INVOKE_ERROR = build("biz.process.failed", BasicCode.BIZ_ERROR, "biz-process-failed", "业务处理失败");
        STANDARD_CODE_STATEMENT_REPEAT = build("standard.code.statement.repeat", BasicCode.TINY_BOOT_ERROR, "code-statement-repeat", "标准代码重复声明");
        STANDARD_CODE_STATEMENT_ERROR = build("standard.code.statement.error", BasicCode.TINY_BOOT_ERROR, "code-statement-error", "标准代码构建失败 缺失关键参数");
    }
}
